<template>
  <el-card class="user-info-card">
    <div v-if="!isEditing">
      <el-descriptions title="用户信息" :column="1">
        <el-descriptions-item label="用户ID">
          <span class="info-value">{{ userId }}</span>
        </el-descriptions-item>
        <el-descriptions-item label="用户名">
          <span class="info-value">{{ userName }}</span>
        </el-descriptions-item>
        <el-descriptions-item label="用户类型">
          <el-tag :type="userTypeTagType" size="small">
            {{ userTypeText }}
          </el-tag>
        </el-descriptions-item>
      </el-descriptions>
      <div class="edit-button">
        <!-- <el-button type="primary" size="small" @click="isEditing = true">修改信息</el-button> -->
      </div>
    </div>

    <!-- <div v-else>
      <div slot="header">
        <span>修改个人信息</span>
      </div>
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item :label="userType === 'organizer' ? '主办方名称' : '队伍名称'" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="原密码" prop="oldPassword">
          <el-input type="password" v-model="form.oldPassword" show-password placeholder="请输入6-14位密码"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input type="password" v-model="form.newPassword" show-password placeholder="请输入6-14位密码"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input type="password" v-model="form.confirmPassword" show-password placeholder="请再次输入6-14位密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="submitForm">保存</el-button>
          <el-button @click="cancelEdit">取消</el-button>
        </el-form-item>
      </el-form>
    </div> -->
  </el-card>
</template>

<script>
import { mapGetters } from 'vuex'

export default {
  name: "UserInfoCard",
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.form.newPassword) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    
    return {
      isEditing: false,
      form: {
        name: '',
        oldPassword: '',
        newPassword: '',
        confirmPassword: ''
      },
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        oldPassword: [
          { required: true, message: '请输入原密码', trigger: 'blur' },
          { min: 6, max: 14, message: '密码长度在6到14个字符', trigger: 'blur' }
        ],
        newPassword: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { min: 6, max: 14, message: '密码长度在6到14个字符', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          { validator: validatePassword, trigger: 'blur' },
          { min: 6, max: 14, message: '密码长度在6到14个字符', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    ...mapGetters(['userType', 'userId', 'userName']),
    userTypeText() {
      switch(this.userType) {
        case 'organizer': return '赛事主办方';
        case 'team': return '龙舟队';
        case 'audience': return '观众';
        default: return '未知类型';
      }
    },
    userTypeTagType() {
      switch(this.userType) {
        case 'organizer': return 'warning';
        case 'team': return 'success';
        case 'audience': return 'info';
        default: return '';
      }
    }
  },
  created() {
    this.resetForm();
  },
  methods: {
    submitForm() {
  this.$refs.form.validate(valid => {
    if (valid) {
      const requestData = {
        [this.userType + 'Id']: this.userId, // 动态生成参数名: organizerId/teamId/audienceId
        name: this.form.name,
        oldPassword: this.form.oldPassword,
        newPassword: this.form.newPassword
      };

      const url = `/${this.userType}/updateInfo`;
      
      this.$axios.post(url, requestData)
      .then(response => {
        if (response.data && response.data.code === 200) {
          this.$message.success('修改成功');
          this.$store.commit('SET_USER_NAME', this.form.name);
          this.isEditing = false;
          
          // 清空密码相关字段
          this.form.oldPassword = '';
          this.form.newPassword = '';
          this.form.confirmPassword = '';
        } else {
          this.$message.error(response.data?.message || '修改失败');
        }
      })
      .catch(error => {
        console.error(error);
        this.$message.error(error.response?.data?.message || '修改失败，请稍后重试');
      });
    }
  });

    },
    cancelEdit() {
      this.isEditing = false;
      this.resetForm();
    },
    resetForm() {
      if (this.$refs.form) {
        this.$refs.form.resetFields();
      }
      this.form.name = this.userName;
    }
  }
}
</script>

<style scoped>
.user-info-card {
  width: 300px;
  margin: 20px;
  transition: all 0.3s;
}

.user-info-card:deep(.el-card__body) {
  padding: 15px;
}

.user-info-card:deep(.el-descriptions__title) {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 15px;
}

.user-info-card:deep(.el-descriptions__label) {
  width: 80px;
  color: #666;
}

.user-info-card:deep(.el-descriptions__content) {
  padding-left: 20px;
}

.info-value {
  font-weight: normal;
}

.edit-button {
  text-align: center;
  margin-top: 15px;
}

/* 不同类型标签的颜色 */
.el-tag--success {
  background-color: #67C23A;
  border-color: #67C23A;
  color: white;
}

.el-tag--warning {
  background-color: #E6A23C;
  border-color: #E6A23C;
  color: white;
}

.el-tag--info {
  background-color: #909399;
  border-color: #909399;
  color: white;
}
</style>